<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class CarController extends Controller
{
	public function getIndex(Request $request)
	{		
		if($request['name']){
	        $res = DB::table('carmanagement')
	            ->join('person','carmanagement.PersonalID','=','person.id')
	            ->where('PersonName','like','%'.$request['name'].'%')
	            ->paginate(3);
	        }else{
	            //所有分页      
	            $res = DB::table('carmanagement')
	            ->join('person','carmanagement.PersonalID','=','person.id')
	            ->paginate(4);
	        }
	        // dd($res);
	       
	        $info = $request->all();

		return view('Lzk.Car.Car',['res'=>$res,'info'=>$info]);
	}

	//解析添加页面
	public function getAdd()
	{
		$add = DB::table('person')->get();
		// dd($add);
		return view('Lzk.Car.AddCar',['add'=>$add]);
	}

	//执行添加
	public function postInsert(Request $request)
	{
		// dd($request->all());
		$ins = $request->only('CarNumber','PersonalID');

		$res = DB::table('carmanagement')->insert($ins);
		if($res){
				return redirect('/CarManagement');
			}else{
				return back();
			}
	}

	//删除
	public function getDel($id)
	{
		$res = DB::select("delete from carmanagement where cid = {$id}");
		// var_dump($del);
		// die;
		if($res){
				return redirect('/CarManagement');
			}else{
				return back();
			}
	}

	//修改解析
	public function getEdit($id)
	{
		$edit = DB::table('carmanagement')->where('cid',$id)->first();
		// dd($edit);
		$edits = DB::select("select id,PersonName from person where {$edit['PersonalID']} = id");
		// dd($edits);
		return view('Lzk.Car.EditCar',['edit'=>$edit,'edits'=>$edits]);	
	}

	//执行修改
	public function postUpdate(Request $request)
	{
		$cid = $request->only('cid');
		// dd($cid);
		$cnumber = $request->only('CarNumber');
		// dd($cnumber);
		$updas = DB::table('carmanagement')->where('cid',$cid)->update($cnumber);
        if($updas){
        	return redirect('/CarManagement')->with('修改成功');
        }else{
        	return back('/Reservation/edit');
        }

	}
}
